package cn.bin758.h5game.module.game.service.record;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.bin758.h5game.module.game.controller.admin.record.vo.*;
import cn.bin758.h5game.module.game.dal.dataobject.record.RecordDO;
import cn.bin758.h5game.framework.common.pojo.PageResult;

import cn.bin758.h5game.module.game.convert.record.RecordConvert;
import cn.bin758.h5game.module.game.dal.mysql.record.RecordMapper;

import static cn.bin758.h5game.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.bin758.h5game.module.game.enums.ErrorCodeConstants.*;

/**
 * 对局信息 Service 实现类
 *
 * @author bin758
 */
@Service
@Validated
public class RecordServiceImpl implements RecordService {

    @Resource
    private RecordMapper recordMapper;

    @Override
    public Long createRecord(RecordCreateReqVO createReqVO) {
        // 插入
        RecordDO record = RecordConvert.INSTANCE.convert(createReqVO);
        recordMapper.insert(record);
        // 返回
        return record.getId();
    }

    @Override
    public void updateRecord(RecordUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRecordExists(updateReqVO.getId());
        // 更新
        RecordDO updateObj = RecordConvert.INSTANCE.convert(updateReqVO);
        recordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecord(Long id) {
        // 校验存在
        this.validateRecordExists(id);
        // 删除
        recordMapper.deleteById(id);
    }

    private void validateRecordExists(Long id) {
        if (recordMapper.selectById(id) == null) {
            throw exception(RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RecordDO getRecord(Long id) {
        return recordMapper.selectById(id);
    }

    @Override
    public List<RecordDO> getRecordList(Collection<Long> ids) {
        return recordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RecordDO> getRecordPage(RecordPageReqVO pageReqVO) {
        return recordMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<RecordDO> getRecordPage2(RecordPageReqVO pageReqVO) {
        return recordMapper.selectPage2(pageReqVO);
    }

    @Override
    public List<RecordDO> getRecordList(RecordExportReqVO exportReqVO) {
        return recordMapper.selectList(exportReqVO);
    }

}
